package com.kitty.game.reward.service;

import com.kitty.common.cache.entity.ActivityNumberSet;
import com.kitty.common.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class RewardSetService {
    public static Map<Integer, List<ActivityNumberSet.RewardSet>> rewardMapByDanBi = new HashMap<>();
    public static List<ActivityNumberSet.RewardSet> rewardMapByLeiji = new ArrayList<>();
    public static List<ActivityNumberSet.RewardSet> rewardMapByLeijika = new ArrayList<>();
    public static Map<Integer, ActivityNumberSet.RewardSet> rewardMapByChouJiang = new HashMap<>();
    @Autowired
    Dao dao;
    @Autowired
    JdbcTemplate jdbcTemplate;
    public void loadProduct() {
        loadRewardByChoujiang();
        loadRewardByDanbi();
        loadRewardByLeiji();
        loadRewardByLeijika();
    }

    /**加载单笔奖励*/
    public void loadRewardByDanbi() {
        Dao dao = SpringUtils.getBean(Dao.class);
        List<ActivityNumberSet.RewardSet> list = dao.query(ActivityNumberSet.RewardSet.class,  Cnd.where("reward_type", "=", "单笔"));
        Map<Integer, List<ActivityNumberSet.RewardSet>> rewardMapByGroup = new HashMap<>();
        for (ActivityNumberSet.RewardSet rewardSet : list) {
            List<ActivityNumberSet.RewardSet> rewardSets = rewardMapByGroup.computeIfAbsent(rewardSet.getMoney(), k -> new ArrayList<>());
            rewardSets.add(rewardSet);
        }
        RewardSetService.rewardMapByDanBi = rewardMapByGroup;
    }

    /**加载累计奖励*/
    public void loadRewardByLeiji() {
        Dao dao = SpringUtils.getBean(Dao.class);
        List<ActivityNumberSet.RewardSet> list = dao.query(ActivityNumberSet.RewardSet.class,  Cnd.where("reward_type", "=", "累计"));
//        Map<Integer, RewardSet> map = new HashMap<>();
//        for (RewardSet rewardSet : list) {
//            map.put(rewardSet.getMoney(),rewardSet);
//        }
        RewardSetService.rewardMapByLeiji = list;
    }

    /**加载充值卡奖励*/
    public void loadRewardByLeijika() {
        Dao dao = SpringUtils.getBean(Dao.class);
        List<ActivityNumberSet.RewardSet> list = dao.query(ActivityNumberSet.RewardSet.class,  Cnd.where("reward_type", "=", "充值卡"));
//        Map<Integer, RewardSet> map = new HashMap<>();
//        for (RewardSet rewardSet : list) {
//            map.put(rewardSet.getMoney(),rewardSet);
//        }
        RewardSetService.rewardMapByLeijika = list;
    }


    /**加载抽奖奖励*/
    public void loadRewardByChoujiang() {
        Dao dao = SpringUtils.getBean(Dao.class);
        List<ActivityNumberSet.RewardSet> list = dao.query(ActivityNumberSet.RewardSet.class,  Cnd.where("reward_type", "=", "抽奖"));
        Map<Integer, ActivityNumberSet.RewardSet> map = new HashMap<>();
        for (ActivityNumberSet.RewardSet rewardSet : list) {
            map.put(rewardSet.getMoney(),rewardSet);
        }
        RewardSetService.rewardMapByChouJiang = map;
    }



    public List<ActivityNumberSet.RewardSet> findRewardList() {
        return dao.query(ActivityNumberSet.RewardSet.class, Cnd.NEW());
    }


    public List<ActivityNumberSet.RewardSet> findRewardListByType(String type) {
        return dao.query(ActivityNumberSet.RewardSet.class, Cnd.where("reward_type","=",type));
    }
    public List<ActivityNumberSet.RewardSet> findRewardListByMoney(Integer money, String type) {
        return dao.query(ActivityNumberSet.RewardSet.class, Cnd.where("money","=",money).and("reward_type","=",type));
    }
}
